﻿define('schedulingModel',
    ['jquery', 'routeConfig', 'ko', 'scheduleType', 'scheduleTemplate'],
    function ($, routeConfig, ko, scheduleType ,scheduleTemplate) {
        var schedulingModel = function() {
            var self = this;
            
            self.departments = ko.observableArray();
            self.selectedDepartment = ko.observable();
            self.providersInDepartment = ko.observableArray();
            self.scheduleTemplates = ko.observableArray();
            self.activeProviderId = ko.observable();
            self.selectedObj = ko.observable(new scheduleTemplate(null, self));
            
            self.scheduleTypes = ko.observableArray([]);
            self.scheduleTypes.removeAll();
            SendAjax("/api/Classifiers/GetScheduleTypes", {}, function(data) {
                $.each(data, function() {
                    self.scheduleTypes.push(new scheduleType(this));
                });
            });
            self.selectedScheduleType = ko.observable();

            self.SelectColor = function() {
                $.each(self.scheduleTypes(), function () {
                    this.selected(false);
                });
                this.selected(true);
                self.selectedScheduleType(this);
            };
           
            SendAjax("/api/Departments/GetLazy", {}, function (data) {
                $.each(data, function() {
                    self.departments.push({ id: this.data, title: this.value });
                });
                var d=self.departments()[0];
                self.selectedDepartment(d.id);
            });

            self.selectedDepartment.subscribe(function() {
                self.LoadProvidersInDepartment(self.selectedDepartment);
            });

            self.LoadProvidersInDepartment = function (departmentId) {
                self.providersInDepartment.removeAll();
                SendAjax("/api/Providers/GetByDepartment", { departmentId: departmentId }, function (data) {
                    $.each(data, function (i) {
                        var o = this;
                        o.selected = ko.observable();
                        if (i == 0) {
                            self.activeProviderId(this.id);
                            o.selected(true);
                            self.LoadScheduleTemplates(self.activeProviderId());
                        }
                        else o.selected(false);
                        self.providersInDepartment.push(o);
                    });
                });
            };

            self.RefreshGrid = function() {
                self.LoadScheduleTemplates(self.activeProviderId());
            };

            self.ProviderIsActive = function(id) {
                if (self.activeProviderId() == id) return true;
                return false;
            };

            self.LoadScheduleTemplates = function(id) {
                self.activeProviderId(id);
                SendAjax("/api/ScheduleTemplates/GetByProvider", { providerId: id }, function (data) {
                    self.scheduleTemplates.removeAll();
                    $.each(data, function (i) {
                        self.scheduleTemplates.push(new scheduleTemplate(this, self.activeProviderId()));
                    });
                });
            };

            self.SelectPerson = function () {
                self.LoadScheduleTemplates(this.id);
                $.each(self.providersInDepartment(), function() {
                    this.selected(false);
                });
                this.selected(true);
            };

        };

        return schedulingModel;
    });